package cn.yuanqiao.manage.service.impl;

import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.manage.domain.YqdaMdataGroup;
import cn.yuanqiao.manage.domain.YqdacategoryModel;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.manage.mapper.YqdaMdataGroupMapper;
import cn.yuanqiao.manage.mapper.YqdacategoryModelMapper;
import cn.yuanqiao.manage.service.IYqdacategoryModelService;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * manageService业务层处理
 * 
 * @author yuanqiao
 * @date 2023-06-14
 */
@Service
public class YqdacategoryModelServiceImpl implements IYqdacategoryModelService
{
    @Autowired
    private YqdacategoryModelMapper yqdacategoryModelMapper;

    @Autowired
    private YqdaMdataGroupMapper yqdaMdataGroupMapper;

    /**
     * 查询manage
     * 
     * @param id manage主键
     * @return manage
     */
    @Override
    public YqdacategoryModel selectYqdacategoryModelByID(Long id)
    {
        return yqdacategoryModelMapper.selectYqdacategoryModelByID(id);
    }

    /**
     * 查询manage列表
     * 
     * @param yqdacategoryModel manage
     * @return manage
     */
    @Override
    public List<YqdacategoryModel> selectYqdacategoryModelList(YqdacategoryModel yqdacategoryModel)
    {
        return yqdacategoryModelMapper.selectYqdacategoryModelList(yqdacategoryModel);
    }

    /**
     * 新增manage
     * 
     * @param yqdacategoryModel manage
     * @return 结果
     */
    @Override
    public int insertYqdacategoryModel(YqdacategoryModel yqdacategoryModel)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }
        if(null==String.valueOf(yqdacategoryModel.getOrderNo())||"".equals(String.valueOf(yqdacategoryModel.getOrderNo()))){
            yqdacategoryModel.setOrderNo(1);
        }
        yqdacategoryModel.setCreator(user.getUsername());
        yqdacategoryModel.setIsDel(0);
        yqdacategoryModel.setCreatorId(user.getUserId());
        yqdacategoryModel.setCreateTime(DateUtils.getNowDate());
        return yqdacategoryModelMapper.insertYqdacategoryModel(yqdacategoryModel);
    }

    /**
     * 修改manage
     * 
     * @param yqdacategoryModel manage
     * @return 结果
     */
    @Override
    public int updateYqdacategoryModel(YqdacategoryModel yqdacategoryModel)
    {
        return yqdacategoryModelMapper.updateYqdacategoryModel(yqdacategoryModel);
    }

    /**
     * 批量删除manage
     * 
     * @param IDs 需要删除的manage主键
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int deleteYqdacategoryModelByIDs(Long[] IDs)
    {
        //元数据方案表修改is_use字段
        for(int i=0;i<IDs.length;i++){
            Long cateId = IDs[i];
            YqdacategoryModel yqdacategoryModel = yqdacategoryModelMapper.selectYqdacategoryModelByID(cateId);
            String groupId = String.valueOf(yqdacategoryModel.getGroupId());
            if(StringUtils.isNotEmpty(groupId) && !"null".equals(groupId)){
                YqdaMdataGroup yqdaMdataGroup = new YqdaMdataGroup();
                yqdaMdataGroup.setIsUse(0);
                yqdaMdataGroup.setId(Long.valueOf(groupId));
                yqdaMdataGroupMapper.updateYqdaMdataGroup(yqdaMdataGroup);
            }
        }
        return yqdacategoryModelMapper.deleteYqdacategoryModelByIDs(IDs);
    }

    /**
     * 删除manage信息
     * 
     * @param ID manage主键
     * @return 结果
     */
    @Override
    public int deleteYqdacategoryModelByID(Long ID)
    {
        return yqdacategoryModelMapper.deleteYqdacategoryModelByID(ID);
    }

    /**
     * 获取门类模板树
     * @param yqdacategorymodel
     * @return
     */
    @Override
    public List<TreeSelect> selectmatemodelTreeList(YqdacategoryModel yqdacategorymodel) {
        List<YqdacategoryModel> list = selectYqdacategoryModelList(yqdacategorymodel);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("门类模板");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdacategoryModel catemodel:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(catemodel.getId());
            chilTree.setLabel(catemodel.getName());
            chilTree.setType(catemodel.getType());
            chilTree.setGroupId(catemodel.getGroupId());
            chilTree.setOrder_no(catemodel.getOrderNo());
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int bindgroup(Long id, Long groupid, Integer wjcls, Integer ajcls, Integer xmcls,Integer type) {
        YqdaMdataGroup yqdaMdataGroup=new YqdaMdataGroup();
        yqdaMdataGroup.setId(groupid);
        yqdaMdataGroup.setWjColLength(wjcls);
        if(1==type||3==type){
            yqdaMdataGroup.setAjColLength(ajcls);
        }
        if (3==type){
            yqdaMdataGroup.setXmColLength(xmcls);
        }
        yqdaMdataGroup.setIsUse(1);
        yqdaMdataGroupMapper.updateYqdaMdataGroup(yqdaMdataGroup);
        YqdacategoryModel yqdacategoryModel=new YqdacategoryModel();
        yqdacategoryModel.setId(id);
        yqdacategoryModel.setGroupId(groupid);
        return yqdacategoryModelMapper.updateYqdacategoryModel(yqdacategoryModel);
    }

    @Override
    public List<TreeSelect> getcatemodeltreehadgroup(YqdacategoryModel yqdacategorymodel) {
        List<YqdacategoryModel> list = selectYqdacategoryModelListhadgroup(yqdacategorymodel);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("门类模板");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(YqdacategoryModel catemodel:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(catemodel.getId());
            chilTree.setLabel(catemodel.getName());
            chilTree.setType(catemodel.getType());
            chilTree.setGroupId(catemodel.getGroupId());
            chilTree.setOrder_no(catemodel.getOrderNo());
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }
    public List<YqdacategoryModel> selectYqdacategoryModelListhadgroup(YqdacategoryModel yqdacategoryModel)
    {
        return yqdacategoryModelMapper.selectYqdacategoryModelListhadgroup(yqdacategoryModel);
    }
}
